package com.sun.tv;

import java.lang.ref.WeakReference;

import android.content.Context;

import com.tools.HttpThrowable;
import com.tools.MyLog;

public class LoadAdRunnable implements Runnable {
	private WeakReference<TVAdView> weakAdViewWindow = null;
	LoadAdRunnable(WeakReference<TVAdView> weakAdViewWindow) {
		this.weakAdViewWindow = weakAdViewWindow;
	}

	@Override
	public void run() {
		TVAdView layout = weakAdViewWindow.get();
		if (layout == null) {
			MyLog.e("load ad fail, HdtMobileAdView is null");
			return;
		}
		Context context = layout.contextWeakReference.get();
		if (context == null) {
			layout.destroy();
			return;
		}
		if (TVAdManage.admanage == null) {
			MyLog.e("load ad fail,admanage is null");
			return;
		}
		try {
			AdConfig config = TVAdManage.admanage.fetchConfig(layout.key);
			if (config == null) {
				return;
			}
			MyLog.v("config download finish");
			try {
				
				TVAdManage.admanage.downloadRes(layout.key, config);
				layout.configFile = config;
				layout.onEvent(IMochaEventCode.adDownloadFinish);
				if (layout.adView != null) {
					layout.adView.updata(layout);
				}
			} catch (HttpThrowable e) {
				layout.onEvent(IMochaEventCode.downloadError);
				MyLog.e(e);
			}

		} catch (Exception e) {
			layout.onEvent(IMochaEventCode.downloadError);
			MyLog.e(e);
		} finally {
			if (layout.adView != null) {
				layout.adView.refreshAd(layout.updataTime);
			}
		}
	}
}
